<?php
/**
 * Created by PhpStorm.
 * User: Digvijay
 * Date: 3/17/14
 * Time: 11:45 AM
 */

class MY_Controller extends CI_Controller {
    public static $em;

    function __construct() {
        parent::__construct();
        if (!$this->session->userdata('logged_in')) {
            redirect('login', 'refresh');
        }
        $this->em = $this->doctrine->em;
        date_default_timezone_set('Asia/Calcutta');
    }

    function registerUserActivty($description) {
        $userActivity = new Useractivity();
        $userActivity->setDate(new DateTime());
        $userActivity->setDescription($description);
        $user = unserialize($this->session->userdata('logged_in'));
        $user = $this->findById("User", $user->getId());
        $userActivity->setUser($user);
        $this->save($userActivity);
    }

    function save($object) {
        $this->em = $this->doctrine->em;
        $this->em->persist($object);
        $this->em->flush();
    }

    function delete($object) {
        $this->em->remove($object);
        $this->em->flush();
    }

    function findById($objectName, $id) {
        return $this->em->getRepository($objectName)->find($id);
    }

    function findAll($objectName) {
        return $this->em->getRepository($objectName)->findAll();
    }

    function getLoggedInUser() {
        return unserialize($this->session->userdata('logged_in'));
    }

    function getDataFor($object, $from, $to, $orderBy) {
        $from = new DateTime($from);
        $to = new DateTime($to);
        $query = $this->em->createQuery("SELECT u FROM " . $object . " u WHERE u.date >= :from AND u.date <= :to ORDER BY u.date " . $orderBy.", u.id ". $orderBy);
        $query->setParameter("from", $from);
        $query->setParameter("to", $to);
        return $query->getResult();
    }

    function getUserActivities() {
        $query = $this->em->createQuery("SELECT u FROM Useractivity u ORDER BY u.date DESC");
        $query->setMaxResults(20);
        return $query->getResult();
    }

    function getUserActivitiesGreaterThan($id = null) {
        if ($id != null) {
            $query = $this->em->createQuery("SELECT u FROM Useractivity u where u.id > :id ORDER BY u.date ASC");
            $query->setParameter("id", $id);
            return $query->getResult();
        }
        return null;
    }

    function findRecent($object) {
        $query = $this->em->createQuery("SELECT o FROM " . $object . " o ORDER BY o.id DESC");
        return count($query->getResult()) > 0 ? $query->getResult()[0] : null;
    }

    function findRecentForDate($date, $object){
        $query = $this->em->createQuery("SELECT o FROM " . $object . " o WHERE o.date = :date ORDER BY o.id DESC");
        $query->setParameter("date", $date);
        return count($query->getResult()) > 0 ? $query->getResult()[0] : null;
    }

    function findGreaterThan($id, $object, $orderBy = "asc") {
        $query = $this->em->createQuery("SELECT u FROM " . $object . " u WHERE u.id > :id ORDER BY u.date " . $orderBy);
        $query->setParameter("id", $id);
        return $query->getResult();
    }

    function findGreaterThanDate($date, $object, $orderBy = "asc") {
        $query = $this->em->createQuery("SELECT u FROM " . $object . " u WHERE  u.date > :date ORDER BY u.date " . $orderBy.", u.id ". $orderBy);
        $query->setParameter("date", $date);
        return $query->getResult();
    }

    function findEqualsOrLessThanDate($date, $object, $orderBy = "asc") {
        $query = $this->em->createQuery("SELECT u FROM " . $object . " u WHERE  u.date <= :date ORDER BY u.date " . $orderBy.", u.id ". $orderBy);
        $query->setParameter("date", $date);
        return $query->getResult();
    }
} 